package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.NanDu;

public class NanDuRowMapper implements RowMapper<NanDu>{
    private int type;
    public static final int NANDU_AND_NUM = 1;
    
    public NanDuRowMapper() {
    }

    public NanDuRowMapper(int type) {
        this.type = type;
    }


    @Override
    public NanDu mapRow(ResultSet rs, int rowNum) throws SQLException {
        NanDu nd = new NanDu();
        switch (type) {
            case NANDU_AND_NUM:
                nd = parseNanDu(new NanDu(), rs);
                nd.setNd_num(rs.getInt("nd_num"));
                break;
            default:
                nd = parseNanDu(new NanDu(), rs);
        }
        return nd;
    }

    public static NanDu parseNanDu(NanDu nd, ResultSet rs)
        throws SQLException {
        return parseNanDu(nd, rs, null);
    }

    public static NanDu parseNanDu(NanDu nd, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            nd.setNd_id(rs.getLong(su + "nd_id"));
        } catch (SQLException e) {
            nd.setNd_id(null);
        }
        try {
            nd.setNd_name(rs.getString(su + "nd_name"));
        } catch (SQLException e) {
            nd.setNd_name(null);
        }
      
        return nd;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            NanDu nd) {
        par.put("nd_id", nd.getNd_id());
        par.put("nd_name", nd.getNd_name());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            NanDu nd) {
        args.put("nd_id", nd.getNd_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final NanDu nd) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(nd),
                        new String[] { "nd_id" });
                parsePS(ps, nd);
                return ps;
            }

        };
    }

    private static String parseSQL(NanDu nd) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_nandu(");
        int count = 0;
        if (null != nd.getNd_id()) {
            sql.append("nd_id,");
            count++;
        }
        if (null != nd.getNd_name()) {
            sql.append("nd_name,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, NanDu nd)
        throws SQLException {
        int count = 1;
        if (null != nd.getNd_id()) {
            ps.setLong(count++, nd.getNd_id());
        }
        if (null != nd.getNd_name()) {
            ps.setString(count++, nd.getNd_name());
        } 

    }

}
